<template>
  <div class="dashboard">
    <el-tabs v-model="tabName" @tab-click="handleTabClick">
      <el-tab-pane
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        :lazy="true"
        label="首页"
        name="首页">
        <el-empty v-if="postList.length === 0" description="暂无相关帖子" />
        <post
          v-for="item in postList"
          v-else
          :key="item.id"
          class="post"
          :post-info="item"
          :like="handleLike"
          :dislike="handleDislike"
          :collect="handleCollect"
          :report="handleReport"
          @click.native="goPostDetail(item)"
        />
      </el-tab-pane>
      <el-tab-pane
        v-loading="loading"
        :lazy="true"
        label="关注"
        name="关注"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading">
        <el-empty v-if="postList.length === 0" description="暂无相关帖子" />
        <post
          v-for="item in postList"
          v-else
          :key="item.id"
          class="post"
          :post-info="item"
          :like="handleLike"
          :dislike="handleDislike"
          :collect="handleCollect"
          :report="handleReport"
          @click.native="goPostDetail(item)"
        />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import post from '@/components/post'
import { savePostFeedback, updatePostFeedback } from '@/api/postfeedback'
import { getPostVoList } from '@/api/post'
import { mapGetters } from 'vuex'
import { getUserFocusInfoList } from '@/api/userFocusInfo'
import pubsub from 'pubsub-js'
export default {
  name: 'DashBoard',
  components: {
    post
  },
  data() {
    return {
      postList: [],
      pageNum: 1,
      pageSize: 5,
      pubsubId: undefined,
      plateId: this.$route.query.plateId ? this.$route.query.plateId : 1,
      tabName: '首页',
      focusUserList: undefined,
      loading: false
    }
  },
  computed: {
    ...mapGetters(['userId'])
  },
  watch: {
    $route() {
      this.plateId = this.$route.query.plateId ? this.$route.query.plateId : 1
      this.getPostVoList()
    }
  },
  beforeMount() {
    this.getPostVoList()
    this.subscribeUpdatePostVoList()
  },
  beforeDestroy() {
    pubsub.unsubscribe(this.pubsubId)
  },
  methods: {
    handleTabClick(tab) {
      this.loading = true
      switch (tab.name) {
        case '首页':
          this.plateId = this.$route.query.plateId ? this.$route.query.plateId : 1
          this.focusUserList = undefined
          this.getPostVoList()
          break
        case '关注':
          if (!this.userId) {
            this.$router.push({
              path: '/login'
            })
          } else {
            this.plateId = undefined
            this.getFocusUserPostVoList()
          }
          break
        default:
          break
      }
    },
    async getFocusUserPostVoList() {
      const { data } = await getUserFocusInfoList()
      this.focusUserList = data
      this.getPostVoList()
    },
    subscribeUpdatePostVoList() {
      // 接收updateDate，获取更多帖子
      this.pubsubId = pubsub.subscribe('updateData', () => {
        this.pageSize += 5
        this.getPostVoList()
      })
    },
    async getPostVoList() {
      this.loading = true
      const userIds = this.focusUserList?.reduce((cur, value) => {
        return [...cur, value.isFocus === 1 ? value.beFocusedUserId : '']
      }, [])
      if (userIds?.length === 0) userIds.push(-1)
      const searchParam = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        searches: [
          {
            key: 'post.status',
            rule: '=',
            value: 1
          },
          {
            key: 'post.plate_id',
            rule: '=',
            value: this.plateId
          },
          {
            key: 'post.user_id',
            rule: 'in',
            value: userIds
          }
        ]
      }
      const { data } = await getPostVoList(searchParam)
      this.postList = data.list
      this.loading = false
    },
    async handleLike(postInfo) {
      const postfeedback = {
        postId: postInfo.id,
        isLike: 0
      }
      if (postInfo.isLike === null && postInfo.isCollect === null) {
        await savePostFeedback(postfeedback)
        this.getPostVoList()
      } else {
        await updatePostFeedback(postfeedback)
        this.getPostVoList()
      }
    },
    async handleDislike(postInfo) {
      const postfeedback = {
        postId: postInfo.id,
        isLike: 1
      }
      if (postInfo.isLike === null && postInfo.isCollect === null) {
        await savePostFeedback(postfeedback)
        this.getPostVoList()
      } else {
        await updatePostFeedback(postfeedback)
        this.getPostVoList()
      }
    },
    async handleCollect(postInfo) {
      const postfeedback = {
        postId: postInfo.id,
        isCollect: postInfo.isCollect === 0 ? 1 : 0
      }
      if (postInfo.isLike === null && postInfo.isCollect === null) {
        await savePostFeedback(postfeedback)
        this.getPostVoList()
      } else {
        await updatePostFeedback(postfeedback)
        this.getPostVoList()
      }
    },
    handleReport(postInfo) {
      this.$router.push({
        path: '/report',
        query: {
          postId: postInfo.id
        }
      })
    },
    goPostDetail(item) {
      this.$router.push({
        path: '/postDetail',
        query: {
          postId: item.id
        }
      })
    }
  }
}
</script>

<style lang="scss">
.dashboard {
  .el-tabs__nav-wrap {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: rgb(255, 255, 255);
    height: 50px;
  }
  .el-tabs__content {
    padding: 10px;
    margin: 10px auto;
    border-radius: 15px;
    width: 1150px;
    min-width: 1150px;
    background: rgba(133, 185, 254, 0.738);
    box-shadow: 0 0 6px black;
    .el-empty__description p {
      color: black;
      font-weight: bold;
    }
  }
  .el-tabs__header {
    margin: 0;
    position: sticky;
    top: 60px;
    z-index: 101;
  }
  .el-loading-mask {
    z-index: 1;
    border-radius: 15px;
    background-image: radial-gradient(white, rgb(212 230 255));
  }
}
</style>

<style lang="scss" scoped>
.dashboard {
  padding: 0 0 10px 0;
  .el-tabs {
    .post {
      margin: 10px auto;
      &:hover {
        cursor: pointer;
        outline: 5px solid rgb(36, 36, 36);
      }
    }
  }
}
</style>
